﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using ProtoBuf.Message;
using Yunchang.NetworkLayer;
using AutoChess;
using Yunchang;

namespace UI
{
    public class ChessFightRecordPage : OPPage
    {
        [SerializeField]
        private Text mTitle;
        [SerializeField]
        private Text mNoneDec;
        [SerializeField]
        private GameObject mNoneDecObj;
        [SerializeField]
        private GameObject mUpArrowObj;
        [SerializeField]
        private GameObject mDownArrowObj;
        [SerializeField]
        private GameObject mFightRecordWaveObj;
        [SerializeField]
        private VerticalLayoutGroup mGrid;
        [SerializeField]
        private ScrollRect mScrollRect;

        private int mCurIndex = 0;
        public int OnceLoadNum;
        private Coroutine mCoroutine = null;

        private RectTransform mSRt;
        private RectTransform mGrt;

        private void Awake()
        {
            mSRt = mScrollRect.GetComponent<RectTransform>();
            mGrt = mGrid.GetComponent<RectTransform>();
        }

        protected override void DoOpen()
        {
            base.DoOpen();

            EventManager.AddEventListener(CommonEvent.ON_AUTOCHESS_SWITCH_BATTLE, Close);
            NetworkDriver.Instance.RegistSocketHandler(OpDefine.SCAutoChessGetFightRecords, OnSCAutoChessFihgtRecord);
            //NetworkDriver.Instance.RegistSocketHandler(OpDefine.SCAutoChessGetFightReplay, OnSCAutoChessGetFightReplay);

            mTitle.text = TableTextBlo.GetText("Chess_FightRecord");
            mNoneDec.text = TableTextBlo.GetText("ACFightRecordNoneDec");
            mNoneDecObj.SetActive(false);
            mUpArrowObj.SetActive(false);
            mDownArrowObj.SetActive(false);
            mFightRecordWaveObj.SetActive(false);
            AutoChessManager.Ins.SendCSAutoChessGetFightRecords();
        }

        protected override void DoClose(bool immediately)
        {
            EventManager.RemoveEventListener(CommonEvent.ON_AUTOCHESS_SWITCH_BATTLE, Close);
            NetworkDriver.Instance.UnregisterSocketHandler(OpDefine.SCAutoChessGetFightRecords, OnSCAutoChessFihgtRecord);
            //NetworkDriver.Instance.UnregisterSocketHandler(OpDefine.SCAutoChessGetFightReplay, OnSCAutoChessGetFightReplay);

            ClearRunCoroutine();

            base.DoClose(immediately);
        }

        private void OnSCAutoChessFihgtRecord(object msg)
        {
            SCAutoChessGetFightRecords data = msg as SCAutoChessGetFightRecords;
            SetFightRecordContent(data);
        }

        //private void OnSCAutoChessGetFightReplay(object msg)
        //{
        //    StaticData.fightSourceType = FightSourceDataType.NetReplay;
        //    GameMain.Instance.ToBattle(FightType.AutoChess);
        //    AutoChessManager.Ins.OnEnterFightLeaveField();
        //    Close();
        //}

        private void SetFightRecordContent(SCAutoChessGetFightRecords data)
        {
            if (data == null)
            {
                mNoneDecObj.SetActive(true);
                mUpArrowObj.SetActive(false);
                mDownArrowObj.SetActive(false);
                return;
            }

            mGrid.transform.DestroyChildren();
            if (data.records.Count < 1)
            {
                mNoneDecObj.SetActive(true);
                mUpArrowObj.SetActive(false);
                mDownArrowObj.SetActive(false);
                return;
            }

            data.records.Sort((r1, r2) => { return r2.wave - r1.wave; });

            mCurIndex = 0;
            ClearRunCoroutine();
            mCoroutine = RunCoroutine.Run(RcSetItemGrid(data));
        }

        IEnumerator RcSetItemGrid(SCAutoChessGetFightRecords data)
        {
            while (true)
            {
                int tempIndex = mCurIndex;
                int upIndex = (tempIndex + OnceLoadNum) > data.records.Count ? data.records.Count : (tempIndex + OnceLoadNum);
                for (int i = tempIndex; i < upIndex; ++i)
                {
                    mCurIndex++;
                    CreateItemObj(data, i);
                }

                yield return new WaitForEndOfFrame();
            }
        }

        private void ClearRunCoroutine()
        {
            if (mCoroutine != null)
            {
                RunCoroutine.Stop(mCoroutine);
                mCoroutine = null;
            }
        }

        private void CreateItemObj(SCAutoChessGetFightRecords data, int index)
        {
            var item = data.records[index];
            if (item.autoChessFightRecordInfos.Count > 0)
            {
                GameObject go = Instantiate<GameObject>(mFightRecordWaveObj, mGrid.transform);
                go.transform.localPosition = Vector3.zero;
                go.transform.localScale = Vector3.one;
                go.SetActive(true);
                go.GetComponent<ChessFightRecordWave>().Init(item);
            }
        }

        public void OnValueChanged()
        {
            float value = mScrollRect.verticalNormalizedPosition;
            if (mGrt.rect.height > mSRt.rect.height)
            {
                if (value <= 0.01f)
                {
                    mUpArrowObj.SetActive(true);
                    mDownArrowObj.SetActive(false);
                }
                else if (value > 0.01f && value < 0.99f)
                {
                    mUpArrowObj.SetActive(true);
                    mDownArrowObj.SetActive(true);
                }
                else if (value > 0.99f)
                {
                    mUpArrowObj.SetActive(false);
                    mDownArrowObj.SetActive(true);
                }
            }
            else
            {
                mUpArrowObj.SetActive(false);
                mDownArrowObj.SetActive(false);
            }
        }
    }
}